package team.zero.lemon.module.workflow.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.service.CrudService;
import team.zero.lemon.module.workflow.dao.IWorkitemDao;
import team.zero.lemon.module.workflow.entity.Parameter.SearchParameter;
import team.zero.lemon.module.workflow.entity.Workitem;

import java.util.List;

/**
 * Created by bolin on 2016/11/16.
 */
@Service
public class WorkitemService extends CrudService<IWorkitemDao, Workitem> {

    @Autowired
    private WorkitemHandlerService workitemHandlerService;


    public Page<Workitem> findTodo(@RequestBody Workitem workitem) {
        try {
            workitem.page.setList(dao.findtodoPage(workitem));
            return workitem.page;
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    @Transactional
    public List<Workitem> findWorkitemByInstanceId(Integer instanceId) {
        List<Workitem> workitems = dao.findWorkitemByInstanceId(instanceId);
        workitems.forEach(w -> {
            w.setWorkitemHandlers(workitemHandlerService.findByWorkitemId(w.getId()));
        });
        return workitems;
    }
}
